<?php
/**
 * Created by PhpStorm.
 * User: kwibon
 * Date: 2018/10/15
 * Time: 17:10
 */

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Request;
use think\Log;


class Commission extends Api
{
    public function _initialize()
    {
        Log::init([
            'type'  =>  'File',
            'path'  =>  APP_PATH.'logs/'
        ]);
    }

    private $api_secret_str = "kwibon2018";

    public function add_commission()
    {
        $post = request()->post();
        if (!empty($post))
        {
            //验证POST字段  secret密钥  secret_code约定字符串
            $match_arr = array(
                "secret",
                "secret_code",
//                "member_id",
//                "member_username",
                "consumption_order_id",
                "consumption_money",
//                "consumption_member_username",
//                "consumption_goods",
                "consumption_time",
                "order_type",
                "order_name"
            );

            $this->addLog($post,"post数据");

            $match_res = $this->match_post($match_arr,$post);
            if($match_res['code']==200)
            {
                if($this->ApiValidate($post['secret_code'],$post['secret']))
                {//验证成功
                    $where['consumption_order_id'] = $post['consumption_order_id'];//订单id
                    $data = db('distributiondetails')->where($where)->find();//查询订单是否已存在
                    if(empty($data))
                    {
                        if(!isset($post['member_username']) && !isset($post['member_id'])){
                            $this->addLog($post,"member_username和member_id至少要有一个");
                            $this->error("member_username和member_id至少要有一个",array(),418);
                        }

                        //查询推荐用户
                        if(isset($post['member_username'])){
                            $member1 = db('member')->where('member_username',$post['member_username'])->find();
                        }else{
                            $member1 = db('member')->where('member_id',$post['member_id'])->find();
                        }

                        if(empty($member1)){
                            $this->addLog($post,"未查询到该username");
                            $this->error("未查询到该username",array(),418);
                        }

                        //存在设置好的返佣金额时
                        if(isset($post['agent_fee'])){
                            //计算用户获得的佣金
                            $money = $post['agent_fee']/100;
                            //对推荐会员的用户做累加
                            db('member')->where('member_id', $member1['member_id'])->setInc('member_money', $money);
                            db('distributiondetails')->insert([
                                'consumption_order_id'=>$post['consumption_order_id'],//订单id
                                'member_id'=>$member1['member_id'],//会员id
                                'consumption_money'=>$post['consumption_money'],//订单金额
                                'consumption_member_id'=>isset($post['consumption_member_id'])?$post['consumption_member_id']:null,//消费用户id
                                'consumption_member_username'=>isset($post['consumption_member_username'])?$post['consumption_member_username']:'',//消费用户username
                                'consumption_goods'=>isset($post['consumption_goods'])?$post['consumption_goods']:null,//商品id
                                'consumption_time'=>strtotime($post['consumption_time']),//订单时间
                                'money'=>$money,//返佣金额
                                'order_type'=>$post['order_type'],//订单类型
                                'order_name'=>$post['order_name'],//订单名称
                            ]);
                        }else{
                            //获取推荐用户的佣金率
                            $commission_per = db('member_type')->where('member_type_id',$member1['member_type_id'])->find();
                            if(in_array($post['order_type'],array('airport','short','fullday','halfday','oneday','around')))
                            {//用车、接送机
                                //订单类型
                                //暂定   304接送机   305包车
                                $order_type_arr = array(
                                    'airport'=>304,
                                    'short'=>305,
                                    'fullday'=>305,
                                    'halfday'=>305,
                                    'oneday'=>305,
                                    'around'=>305
                                );
                                if(!isset($order_type_arr[$post['order_type']])){
                                    $this->addLog($post,"订单类型错误");
                                    $this->error("订单类型错误",array(),415);
                                }
                                $classify_id = $order_type_arr[$post['order_type']];
                            }else{
                                if(isset($post['consumption_goods'])){
                                    $goods = db("goods")->where("goods_id",$post['consumption_goods'])->field("classify_id")->find();
                                    $classify_id = $goods['classify_id'];
                                }else{
                                    $this->addLog($post,"该类型订单不能缺少商品ID");
                                    $this->error("该类型订单不能缺少商品ID",array(),422);
                                }
                            }

                            //拆分 返佣利率字符串
                            $commission_per_list = $commission_per['commission_per_list'];

                            $log[]=[
                                'commission_per_list'=>$commission_per_list,
                                'classify_id'=>$classify_id,
                            ];
                            Log::record($log);

                            if($commission_per_list==''){
                                $this->addLog($post,"尚未设置该会员等级返佣利率");
                                $this->error("尚未设置该会员等级返佣利率",array(),417);
                            }

                            $classify_info = Db::name('classify')->where("classify_status",1)->select();
                            //加入包车和接送机
                            $classify_info[] = array("classify_id"=>304,"classify_name"=>"接送机","classify_status"=>1,"creattime"=>0,"updatetime"=>0);
                            $classify_info[] = array("classify_id"=>305,"classify_name"=>"包车","classify_status"=>1,"creattime"=>0,"updatetime"=>0);
                            $classArr = array();
                            foreach ($classify_info as $v) {
                                $classArr[] = $v['classify_id'];
                            }

                            $commission_per_arr = explode(",",$commission_per_list);
                            $all_per_arr = array();
                            foreach ($commission_per_arr as $per_value){
                                $per_arr = explode("_",$per_value);
                                $per_classify_id = $per_arr[0];
                                if(!in_array($per_classify_id,$classArr)){
                                    continue;
                                }
                                $per = $per_arr[1];
                                $all_per_arr[$per_classify_id] = $per;
                            }

                            if(!isset($all_per_arr[$classify_id])) {
                                $this->addLog($post,"未设置该类型返佣利率");
                                $this->error("未设置该类型返佣利率",array(),416);
                            }else{
                                if ($all_per_arr[$classify_id] < 0 || $all_per_arr[$classify_id] > 1) {

                                } else {
                                    //计算用户获得的佣金
                                    $money = $post['consumption_money']*$all_per_arr[$classify_id];
                                    //对推荐会员的用户做累加
                                    db('member')->where('member_id', $member1['member_id'])->setInc('member_money', $money);
                                    db('distributiondetails')->insert([
                                        'consumption_order_id'=>$post['consumption_order_id'],//订单id
                                        'member_id'=>$member1['member_id'],//会员id
                                        'consumption_money'=>$post['consumption_money'],//订单金额
                                        'consumption_member_id'=>isset($post['consumption_member_id'])?$post['consumption_member_id']:null,//消费用户id
                                        'consumption_member_username'=>isset($post['consumption_member_username'])?$post['consumption_member_username']:'',//消费用户username
                                        'consumption_goods'=>isset($post['consumption_goods'])?$post['consumption_goods']:null,//商品id
                                        'consumption_time'=>strtotime($post['consumption_time']),//订单时间
                                        'money'=>$money,//返佣金额
                                        'order_type'=>$post['order_type'],//订单类型
                                        'order_name'=>$post['order_name'],//订单名称
                                    ]);
                                }
                            }
                        }
                        $this->addLog($post,"存表成功");
                        $this->success("存表成功",array(),200);
                    }else{
                        $this->addLog($post,"订单已存在");
                        $this->error("订单已存在",array(),410);
                    }
                }else{
                    $this->addLog($post,"密令错误");
                    $this->error("密令错误",array(),412);
                }
            }else{
                $this->addLog($post,"POST字段缺少");
                $this->error("POST字段缺少",array('missKey'=>$match_res['missKey']),$match_res['code']);
            }
        }else{
            $this->addLog($post,"无数据传入");
            $this->error("无数据传入",array(),413);
        }
    }


    public function addLog($post,$msg){
        $log[]=[
            '订单系统调取返佣接口数据'=>$post,
            '订单系统调取返佣接口msg'=>$msg,
        ];
        Log::record($log);
    }


}
















